//
//  GenerateViewController.m
//  QRCode
//
//  Created by My Mac on 2017/5/29.
//  Copyright © 2017年 MyMac. All rights reserved.
//

#import "GenerateViewController.h"

@interface GenerateViewController ()
{
    __weak IBOutlet UITextField *contentQRCode;
    
    __weak IBOutlet UIImageView *imgViewQRCode;
    
    __weak IBOutlet UILabel *labelQRCode;
}

@end

@implementation GenerateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];
    longPress.minimumPressDuration = 0.6;
    [imgViewQRCode addGestureRecognizer:longPress];
}
#pragma mark - 长按
-(void) longPress:(UILongPressGestureRecognizer *)gesture
{
    //判断手势发生变化
    if (gesture.state == UIGestureRecognizerStateBegan) {
        UIImageView *imageView = (UIImageView *)gesture.view;
        [self setShowAlert:imageView.image];
    }
}

-(void)setShowAlert:(UIImage *)image
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"是否保存图片" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"保存图片到相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotoAlbum:didfinishSavingWithError:contextInfo:), nil);
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

//判断图片是否保存到相册
-(void)imageSavedToPhotoAlbum:(UIImage *)image didfinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (!error) {
        [self.view makeToast:@"保存成功" duration:0.5 position:@"bottom"];
    } else {
        [self.view makeToast:error.description duration:0.5 position:@"bottom"];
    }
}

- (IBAction)generateQRCode:(id)sender {
    imgViewQRCode.image = [self generateContentQRCode:contentQRCode.text];
    if (imgViewQRCode.image == nil) {
        labelQRCode.hidden = YES;
    } else {
        labelQRCode.hidden = NO;
    }
}

-(UIImage *)generateContentQRCode:(NSString *)codeContent
{
    CIImage *ciImage;
    //将输入的文字转为NSData类型才能显示二维码
    NSData *data = [codeContent dataUsingEncoding:NSUTF8StringEncoding];
    //图片处理类 : CIQRCodeGenerator
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    //设置内容 : inputMessage
    [filter setValue:data forKey:@"inputMessage"];
    //设置容错率 H : inputCorrectionLevel
    [filter setValue:@"H" forKey:@"inputCorrectionLevel"];
    //将设置好的filter赋值给次Image
    ciImage = filter.outputImage;
    
    //图片消除模糊 放大图片
    CGFloat sacleX = 300;
    CGFloat sacleY = 300;
    CIImage *transImage = [ciImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, sacleX, sacleY)];
    
    //渲染图片
    UIImage *image = [UIImage imageWithCIImage:transImage];
    //绘制图片上下文,并设置图片大小
    UIGraphicsBeginImageContext(CGSizeMake(300, 300));
    //画布
    [image drawInRect:CGRectMake(0, 0, 300, 300)];
    //绘制后的图片
    image = UIGraphicsGetImageFromCurrentImageContext();
    //结束绘制
    UIGraphicsEndImageContext();
    
    //返回处理后的二维码
    return image;
}
@end
